package xy.come.juhenews.Fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import xy.come.juhenews.Adapter.VideoListAdapter;
import xy.come.juhenews.Base.BaseFragment;
import xy.come.juhenews.Bean.GetMP4DataBean;
import xy.come.juhenews.R;
import xy.come.juhenews.Uitils.ConvertUtils;
import xy.come.juhenews.Uitils.LoadingDialog;


/**
 * Created by Xia_焱 on 2018/7/11.
 * e-mail：XiahaotianV@163.com
 */

public class VideoFragment extends BaseFragment {


    @BindView(R.id.rc_list)
    RecyclerView rcList;

    private VideoListAdapter mAdapter;
    private LoadingDialog loadingDialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_video;
    }

    @Override
    public void initData() {

        GetMP4Data();

    }


    @Override
    protected void initView() {
        loadingDialog = new LoadingDialog(getActivity());
        setNavigationBar(getActivity());
    }


    @Override
    protected void lazyLoad() {

    }


    /**
     * 获取MP4数据
     */
    private void GetMP4Data() {
        Gson gson = new Gson();
        try {
            String json = ConvertUtils.toString(getActivity().getAssets().open("expend.json"));
            GetMP4DataBean data = gson.fromJson(json, new TypeToken<GetMP4DataBean>() {
            }.getType());
            List<GetMP4DataBean.DataBean.ChildBean> info = data.getData().getChild();
            initAdapter(info);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void initAdapter(List<GetMP4DataBean.DataBean.ChildBean> videoDataList) {
        rcList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new VideoListAdapter(R.layout.item_detail_video, videoDataList);
        mAdapter.openLoadAnimation();
        rcList.setAdapter(mAdapter);

    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void stopLoad() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.stopLoad();

    }


    private void setNavigationBar(FragmentActivity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.main_color));
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

    }


}
